﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;

namespace QLTV_V2
{
    public partial class FrmDKMaCB : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmDKMaCB()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        SachBUS sachBUS = new SachBUS();
        DauSach ds = new DauSach();
        BanSach bs = new BanSach();
        public string madausach, maCTPhieuNhap;
        public int dkmaCB;
        private void FrmDKMaCB_Load(object sender, EventArgs e)
        {
            dgv_MaCB.DataSource = sachBUS.getBanSach(madausach);
        }
        private BanSach getBanSach_object()
        {
            ds = sachBUS.getDauSach_object("tendausach", "maloaisach", madausach, 2);//Lấy thông tin đầu sách theo mã đầu sách
            string lastID = uti.GetLastID_DK("BanSach", "MaDauSach", ds.MaDauSach, "MaBanSach", "MaBanSach", ds.MaDauSach + "-0000");
            string nextID = uti.GetNextID(lastID, ds.MaDauSach + "-");
            bs.MaBanSach = nextID.ToUpper();
            bs.MaDauSach = madausach;
            bs.NgayCN = System.DateTime.Now;
            bs.MaViTri = ds.ViTriTam;
            bs.TinhTrang = "Tốt";
            bs.TrangThai = "Sẵn sàng";
            return bs;
        }
        private void btn_TaoMaCB_Click(object sender, EventArgs e)
        {
            if (dkmaCB != 1)
            {
                ds = sachBUS.getDauSach_object("s", "s", madausach, 2);
                int soluong = ds.SoLuongTam;
                int kt = 0;
                for (int i = 0; i < soluong; i++)
                {
                    bs = getBanSach_object();
                    if (sachBUS.insertBanSach(bs) == true)
                        kt++;
                }
                if (kt != 0)
                {
                    if (sachBUS.update_After_BM_DK(maCTPhieuNhap, 2) == true)
                    {
                        MessageBox.Show("Tạo thành công " + kt + " mã cá biệt", "Thông báo");
                        dgv_MaCB.DataSource = sachBUS.getBanSach(madausach);
                    }
                }
            }
            else
                MessageBox.Show("Sách này đã được đăng kí mã cá biệt, không thể đăng ký!", "Thông báo");
        }
        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            string masach = madausach;
            int dong = dgv_MaCB.CurrentCell.RowIndex;
            string macb = dgv_MaCB.Rows[dong].Cells["MaBanSach"].Value.ToString();
            if (MessageBox.Show("Xóa " + macb + " không ?", "Hỏi", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (uti.deleteTable("BanSach", "MaBanSach", macb) == true)
                    dgv_MaCB.DataSource = sachBUS.getBanSach(masach);
            }
            else
                MessageBox.Show("Không thể xóa", "Thông báo");
        }

        private void dgv_MaCB_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_MaCB);
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
